<?php
// +----------------------------------------------------------------------
// | User: zq
// +----------------------------------------------------------------------
// | Time: 2021/12/1 09:59
// +----------------------------------------------------------------------

namespace App\Services\User;

use App\CodeResponse;
use App\Exceptions\BusinessException;
use App\Models\User\User;
use App\Notifications\VerificationCode;
use App\Services\BaseService;
use Carbon\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Notification;
use Leonis\Notifications\EasySms\Channels\EasySmsChannel;
use Overtrue\EasySms\PhoneNumber;

class UserService extends BaseService
{
    public function getUserById($id)
    {
        return User::query()->find($id);
    }

    public function getUsers(array $userIds)
    {
        if (empty($userIds)) {
            return collect([]);
        }
        return User::query()->whereIn('id', $userIds)
            ->get();
    }

    /**
     * 通过用户名查找用户
     * @param $username
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getByUsername($username)
    {
        // 查询用户
        return User::query()->where('username', $username)
            ->first();
    }

    /**
     * 通过号码查询用户
     * @param $mobile
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getByMobile($mobile)
    {
        return User::query()->where('mobile', $mobile)
            ->first();
    }

    /**
     * 防刷验证, 当天只能请求10次
     * @param  string  $mobile
     * @return bool
     */
    public function checkMobileSendCaptchaCount(string $mobile)
    {
        $countKey = 'register_captcha_count_' . $mobile;
        if (Cache::has($countKey)) {
            $count = Cache::increment($countKey);
            if ($count > 10) {
                return false;
            }
        } else {
            Cache::put($countKey, 1, Carbon::tomorrow()->diffInSeconds(now()));
        }

        return true;
    }

    /**
     * 发送短信
     * @param  string  $mobile
     * @param  string  $code
     */
    public function sendCaptchaMsg(string $mobile, string $code)
    {
        // 验证环境是否是local
        if (app()->environment('local')) {
            return;
        }

        // 发送短信
        Notification::route(
            EasySmsChannel::class,
            new PhoneNumber($mobile, 86)
        )->notify(new VerificationCode($code));
    }

    /**
     * 验证短信验证码是否有效
     * @param  string  $mobile
     * @param  string  $code
     * @return bool
     */
    public function checkCaptcha(string $mobile, string $code)
    {
        $key = 'register_captcha_' . $mobile;
        $isPass = $code === Cache::get($key);
        // 存在则删除,只能用一次
        if ($isPass) {
            Cache::forget($key);
            return true;
        } else {
            throw new BusinessException(CodeResponse::AUTH_CAPTCHA_UNMATCH);
        }
    }

    /**
     * 生成短信验证码
     * @param  string  $mobile
     * @return string
     */
    public function setCaptcha(string $mobile): string
    {
        // 随机生成6位验证码
        $code = strval(random_int(100000, 999999));

        // 非生产环境固定短信验证码
        if (!app()->environment('production')) {
            $code = '111111';
        }

        // 保存手机号和验证码的关系, 存入redis, 时间 600秒
        Cache::put('register_captcha_' . $mobile, $code, 600);
        return $code;
    }
}